package org.ns.summer.aop;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MethodInvokeInterceptor implements MethodInterceptor {
    private Object target;
    private Class<?> targetClass;

    private org.ns.summer.aop.MethodInterceptor methodInterceptor;

    public MethodInvokeInterceptor(Object target, Class<?> targetClass, org.ns.summer.aop.MethodInterceptor methodInterceptor) {
        this.target = target;
        this.targetClass = targetClass;
        this.methodInterceptor = methodInterceptor;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        if (method.getName().equals("getTargetClass") && method.getParameterTypes().length == 0) {
            return targetClass;
        }
        return methodInterceptor.invoke(new MethodInvoke(obj, target, args, method, proxy));
    }

}
